package com.hank.tetris;


import lombok.Getter;
import lombok.Setter;

import java.awt.*;
import java.util.Random;

/**
 * 定义方块
 * 四个方块,7中形态
 */


@Setter
@Getter
public abstract class Tetromino{
    protected Cell[] cells = new Cell[4];//定义四个格子,静态方法
    protected State[] states;

    /*旋转状态序列号， states = index % states.length*/
    protected int roatenumber = 10000;

    //记录每种翻转形态的状态
    protected class State{
        int col0,row0,col1,row1,col2,row2,col3,row3;

        public State(int row0,int col0, int row1, int col1,int row2, int col2, int row3,int col3) {
            this.row0 = row0;
            this.col0 = col0;
            this.row1 = row1;
            this.col1 = col1;
            this.row2 = row2;
            this.col2 = col2;
            this.row3 = row3;
            this.col3 = col3;
        }
    }

    //向左翻转
    public void roateRight(){
        this.roatenumber++;

        int index =this.roatenumber % states.length;
        State s = states[index];

        System.out.println("index++" + this.roatenumber + "\tstates长度：" + states.length + "\t取余数 " + index);
        System.out.println("row1："+s.row1+"col1："+s.col1+"row2："+s.row2+"col2："+s.col2+"row3："+s.row3+"col3："+s.col3);
        Cell o=cells[0];
        int row = o.getRow();
        int col = o.getCol();
        cells[1].setRow(row+s.row1);
        cells[2].setRow(row+s.row2);
        cells[3].setRow(row+s.row3);
        cells[1].setCol(col+s.col1);
        cells[2].setCol(col+s.col2);
        cells[3].setCol(col+s.col3);
    }

    //向右边翻转
    public void roateLeft(){
        this.roatenumber++;
        int index =this.roatenumber % states.length;
        State s = states[index];
        System.out.println("index++" + this.roatenumber + "\tstates长度：" + states.length + "\t取余数 " + index);
        System.out.println("row1："+s.row1+"col1："+s.col1+"row2："+s.row2+"col2："+s.col2+"row3："+s.row3+"col3："+s.col3);
        Cell o = cells[0];
        int row = o.getRow();
        int col = o.getCol();
        cells[1].setRow(row + s.row1);
        cells[2].setRow(row + s.row2);
        cells[3].setRow(row + s.row3);
        cells[1].setCol(col + s.col1);
        cells[2].setCol(col + s.col2);
        cells[3].setCol(col + s.col3);
    }

    //向左
    public void moveLeft(){
        for(int i=0;i<cells.length;i++){
            cells[i].moveLeft();
        }
    }

    //右移动
    public void moveRight(){
        for(int i=0;i<cells.length;i++){
            cells[i].moveRight();
        }
    }

    //向下
    public void softDorp(){
        for(int i=0;i<cells.length;i++){
            cells[i].drop();
        }
    }

    //绘制图片

    /**
     * 工厂方法,产生四个随机方块
     * @return
     */
    public static Tetromino getOne(){
        Random random = new Random();
        int type = random.nextInt(7);
        switch (type){
            case 0:
                return new T();
            case 1:
                return new I();
            case 2:
                return new S();
            case 3:
                return new J();
            case 4:
                return new L();
            case 5:
                return new Z();
            case 6:
                return new O();
        }
        return null;
    }
}

//7中图形T,I,Z,S,L,J,O
//cells[0]为中心点

/**
 * T格子
 */
class T extends Tetromino{
    public T(){
        //初始状态
        cells[0] = new Cell(0,4, new Color(128,0,128));
        cells[1] = new Cell(0,3,new Color(128,0,128));
        cells[2] = new Cell(0,5,new Color(128,0,128));
        cells[3] = new Cell(-1,4,new Color(128,0,128));

        //顺时针旋转的顺序状态:
        states = new State[]{
                new State(0,0,0,-1,0,1,-1,0),
                new State(0,0,-1,0,1,0,0,1),
                new State(0,0,0,-1,0,1,1,0),
                new State(0,0,0,-1,-1,0,1,0),
        };
    }
}

/**
 * I格子
 */
class I extends Tetromino{
    public I(){
        cells[0]=new Cell(0,4,new Color(107, 255, 250));
        cells[1]=new Cell(0,3,new Color(107, 255, 250));
        cells[2]=new Cell(0,5,new Color(107, 255, 250));
        cells[3]=new Cell(0,6,new Color(107, 255, 250));

        states = new State[]{
                new State(0,0,0,-1,0,1,0,2),
                new State(0,0,-1,0,1,0,2,0),
        };
    }
}

/**
 * Z格子
 */
class Z extends Tetromino{
    public Z(){
        cells[0]=new Cell(1,4,new Color(255,0,0));
        cells[1]=new Cell(0,3,new Color(255,0,0));
        cells[2]=new Cell(0,4,new Color(255,0,0));
        cells[3]=new Cell(1,5,new Color(255,0,0));

        states = new State[]{
                new State(0,0,-1,-1,-1,0,0,1),
                new State(0,0,-1,1,0,1,1,0),
        };
    }
}

/**
 * S格子
 */
class S extends Tetromino{
    public S(){
        cells[0] = new Cell(1,4,new Color(0, 255, 0));
        cells[1] = new Cell(1,3,new Color(0, 255, 0));
        cells[2] = new Cell(0,4,new Color(0, 255, 0));
        cells[3] = new Cell(0,5,new Color(0, 255, 0));

        states = new State[]{
                new State(0,0,0,-1,-1,0,-1,1),
                new State(0,0,-1,0,0,1,1,1),
        };
    }
}

/**
 * L格子
 */
class L extends  Tetromino{
    public L(){
        cells[0]=new Cell(0,4,new Color(0, 0, 255));
        cells[1]=new Cell(0,3,new Color(0, 0, 255));
        cells[2]=new Cell(0,5,new Color(0, 0, 255));
        cells[3]=new Cell(1,3,new Color(0, 0, 255));

        states = new State[]{
                new State(0,0,0,1,0,-1,-1,1),
                new State(0,0,1,0,-1,0,1,1),
                new State(0,0,0,1,0,-1,1,-1),
                new State(0,0,-1,0,1,0,-1,-1),
        };
    }
}

/**
 * J格子
 */
class J extends  Tetromino{
    public J(){
        cells[0] = new Cell(0,4,new Color(250,128,114));
        cells[1] = new Cell(0,3,new Color(250,128,114));
        cells[2] = new Cell(0,5,new Color(250,128,114));
        cells[3] = new Cell(1,5,new Color(250,128,114));

        states = new State[]{
                new State(0,0,0,-1,0,1,1,1),
                new State(0,0,-1,0,1,0,1,-1),
                new State(0,0,0,1,0,-1,-1,-1),
                new State(0,0,1,0,-1,0,-1,1),
                };
    }
}

/**
 * O格子
 */
class O extends Tetromino{
    public O(){
        cells[0]= new Cell(0,4,new Color(255, 255, 0));
        cells[1]= new Cell(0,5,new Color(255, 255, 0));
        cells[2]= new Cell(1,4,new Color(255, 255, 0));
        cells[3]= new Cell(1,5,new Color(255, 255, 0));
    }
}
